/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.mvc.controller.user;

import com.workcell.workdata.im.annotation.UID;
import com.workcell.workdata.im.entity.User;
import com.workcell.workdata.im.model.response.ResponseEntity;
import com.workcell.workdata.im.model.vo.AccountDataVO;
import com.workcell.workdata.im.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/account")
@Api(produces = "application/json", tags = "基础数据相关接口")
public class AccountController {
 
	@Resource
	private GroupService groupService;

	@Resource
	private FriendService friendService;

	@Resource
	private MicroServerService microServerService;

	@Resource
	private MomentRuleService momentRuleService;

	@Resource
	private EmoticonService emoticonService;

	@Resource
	private UserService userService;

	@Resource
	private OrganizationService organizationService;

	@GetMapping(value = "/data")
	@ApiOperation(httpMethod = "GET", value = "登录后获取初始数据(新)",notes = "[好友列表，群组列表，公众号列表，组织联系人，朋友圈权限配置]")
	public ResponseEntity<AccountDataVO> getVo(@ApiIgnore @UID Long uid) {

		AccountDataVO vo = new AccountDataVO();

		vo.setGroupList(groupService.getRelevantList(uid));

		vo.setMicroServerList(microServerService.findList(uid));

		vo.setEmoticonList(emoticonService.findList(uid));

		vo.setMomentIgnoreList(momentRuleService.findMeIgnoreList(uid));

		User user = userService.findOne(uid);

		Map<Long,String> friendAliasMap = friendService.findAliasMap(user.getId());

		if (user.getOrganizationId() == null){
			vo.setFriendList(userService.findVoList(friendAliasMap.keySet()),friendAliasMap);
			return ResponseEntity.ok(vo);
		}

		vo.setOrganization(organizationService.findOne(user.getOrganizationId()));

		List<Long> orgUidList = organizationService.findUidList(user.getOrganizationId());

		Collection<Long> friendUidList = friendAliasMap.keySet();

		Collection<Long> bothUidList = CollectionUtils.intersection(orgUidList,friendUidList);

		Collection<Long> onlyFriendUidList = CollectionUtils.subtract(friendUidList,orgUidList);

		Collection<Long> onlyOrgUidList = CollectionUtils.subtract(orgUidList,friendUidList);

		vo.setContactList(userService.findVoList(onlyOrgUidList),friendAliasMap);

		vo.setFriendList(userService.findVoList(onlyFriendUidList),friendAliasMap);

		vo.setBothList(userService.findVoList(bothUidList),friendAliasMap);

		return ResponseEntity.ok(vo);
	}


}
